#include "plugin_api.h"
#include <dlfcn.h>
#include <iostream>

int main()
{
    void* handle = dlopen("/data/TUTORIAL/concurrency_parallelism/bazel-bin/load_shared_lib/example2_plugin.so", RTLD_NOW);
    if (!handle) {
        std::cerr << dlerror() << "\n";
        return 1;
    }
    // 清除旧错误
    dlerror();
    // 获取工厂
    using create_t = PluginBase* (*)();
    using destroy_t = void (*)(PluginBase*);
    create_t create = reinterpret_cast<create_t>(dlsym(handle, "create"));
    destroy_t destroy = reinterpret_cast<destroy_t>(dlsym(handle, "destroy"));
    const char* err = dlerror();
    if (err) {
        std::cerr << "dlsym error: " << err << "\n";
        dlclose(handle);
        return 1;
    }

    PluginBase* p = create();
    std::cout << p->name() << std::endl;
    p->run();

    destroy(p);
    dlclose(handle);

    return 0;
}
